import 'package:bt_dsp_88/tool/assets.dart';
import 'package:flutter/material.dart';

import '../../model/model.dart';

abstract class BaseGroupGrid extends StatefulWidget {
  List<List<GroupModel>> list;
  void Function(GroupModel) onClick;
  BaseGroupGrid({super.key, required this.list, required this.onClick});

  @override
  State<BaseGroupGrid> createState();
}

abstract class BaseGroupGridState<T extends BaseGroupGrid> extends State<T> {
  double item_gap = 10;
  double row_gap = 5;
  List<List<Widget>> buildContent();

  ///
  List<Widget> _getSlivers(
      List<List<Widget>> list, double itemH, double width) {
    List<Widget> result_sliver = [];
    for (var sub_list in list) {
      result_sliver.add(_get_grid(sub_list, itemH, width));

      ///添加间隔
      result_sliver.add(
        SliverToBoxAdapter(
          child: SizedBox(height: row_gap),
        ),
      );
    }
    return result_sliver;
  }

  SliverGrid _get_grid(List<Widget> list, double itemH, double width) {
    double w = (width - (list.length - 1) * item_gap) / list.length;
    return SliverGrid.extent(
      ///交叉轴最大的尺寸
      maxCrossAxisExtent: w,
      crossAxisSpacing: item_gap,
      mainAxisSpacing: 0,
      childAspectRatio: w * 1.0 / (list.length < 4 ? (itemH - 5) : itemH),
      children: list.map(($0) {
        return $0;
      }).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    List<List<Widget>> _list = buildContent();
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constrains) {
      double h = 40 * GlobalConfig.scaleH;
      return SizedBox(
        ///如果有多行，首行固定35高度
        height: _list.length <= 1
            ? _list.length * (h + row_gap)
            : (35 * GlobalConfig.scaleH +
                (_list.length - 1) * (h + row_gap) +
                5),
        child: CustomScrollView(
          physics: const NeverScrollableScrollPhysics(),
          slivers: _getSlivers(_list, h, constrains.maxWidth),
        ),
      );
    });
  }
}
